#include "krol.h"
#include "plansza.h"
#include "wieza.h"
Krol::Krol(int x, int y)
    :Figura(x, y, KROL)
{
    this->czyAktywna=true;
    this->Graf='K';
    this->Czyruch = 0;
}

bool Krol::czyMogeIsc(int x, int y, Plansza *plansza)
{
    if(x >= 9 || x < 1 || y >=9 || y < 1 )
        return false;
    if(plansza->aktywnyGracz->czyToJestMojaFigura(plansza->GetFiguraAt(x,y)))
        return false;
    // roszada
    if(x - this->x == 2 && this->Czyruch == 0 && y == this->y && plansza->czySzach(plansza->przeciwnik, plansza->aktywnyGracz) == false)
    {
        Figura * tmp = plansza->GetFiguraAt(this->x + 3,this->y);
        if(tmp->getTyp() == WIEZA)
         {
            Wieza * tmpW = (Wieza*) tmp;
            if(tmpW->GetCzyruch() == 0)
            {
                std::vector<Figura *> *vecFig = plansza->przeciwnik->GetVectorFigut();
                for(int i = this->x +1 ; i <= x ; i++)
                {
                    for(int j = 0 ; j < (*vecFig).size() ; j++ )
                    {
                        if((*vecFig)[j]->czyMogeBic(i,this->y,plansza))
                            return false;
                    }
                }
                for(int i = tmp->GetX() -1 ; i > this->x ; i--)
                {
                    if(plansza->GetFiguraAt(i,this->y) != NULL)
                        return false;
                }
                tmp->idz(this->x+1,this->y);
                return true;
            }
        }
    }

    if(x - this->x == -2 && this->Czyruch == 0 && y == this->y && plansza->czySzach(plansza->przeciwnik, plansza->aktywnyGracz) == false)
    {
        Figura * tmp = plansza->GetFiguraAt(this->x - 4,this->y);
        if(tmp->getTyp() == WIEZA)
        {
            Wieza * tmpW = (Wieza*) tmp;
            if(tmpW->GetCzyruch() == 0)
            {
                std::vector<Figura *> *vecFig = plansza->przeciwnik->GetVectorFigut();
                for(int i = this->x -1 ; i >= x ; i--)
                {
                    for(int j = 0 ; j < (*vecFig).size() ; j++ )
                    {
                        if((*vecFig)[j]->czyMogeBic(i,this->y,plansza))
                            return false;
                    }
                }
                for(int i = tmpW->GetX() +1 ; i < this->x ; i++)
                {
                    if(plansza->GetFiguraAt(i,this->y) != NULL)
                        return false;
                }
                tmpW->idz(this->x-1,this->y);
                return true;
            }
        }
    }
        // koniec roszady
    if(((x - this->x) == 1 && (y - this->y) == 1 )|| ((x - this->x) == -1 &&  (y - this->y) == -1) || ((x - this->x) == 1 && (y - this->y) == -1 ) || ((x - this->x) == -1 && (y - this->y == 1)) || (this->y == y && (x - this->x) == 1) || (this->y == y && (x - this->x) == - 1) || (this->x == x && (y - this->y) == 1) || (this->x == x && (y - this->y) == -1))
       {
            return true;
       }


    return false;
}

bool Krol::czyMogeBic(int x, int y, Plansza *plansza)
{
    return czyMogeIsc(x,y,plansza);
}

void Krol::SetCzyruch(int ruch)
{
    this->Czyruch=ruch;
}

int Krol::GetCzyruch()
{
    return this->Czyruch;
}

void Krol::SetPrevCzyruch(int prevCzyruch)
{
    this->PrevCzyruch=prevCzyruch;
}

int Krol::GetPrevCzyruch()
{
    return this->PrevCzyruch;
}
